package com.cc.mall.service.impl;

import com.cc.mall.vo.CatalogVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cc.mall.dao.ProductCategoryDao;
import com.cc.mall.entity.ProductCategoryEntity;
import com.cc.mall.service.ProductCategoryService;


@Service("productCategoryService")
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryDao, ProductCategoryEntity> implements ProductCategoryService {


    @Override
    public List<CatalogVo> getCategory() {
        List<CatalogVo> catalogVoList1 = new ArrayList<>();
        List<ProductCategoryEntity> productCategoryList = baseMapper.selectList(null);
        catalogVoList1 = productCategoryList.stream().filter(f -> f.getPid() == 0).map(m -> {
            CatalogVo catalogVo1 = new CatalogVo();

            catalogVo1.setId(m.getId());
            catalogVo1.setName(m.getProductCategoryName());
            catalogVo1.setChildren(getCatagoryChildren(productCategoryList,catalogVo1));

            return catalogVo1;
        }).collect(Collectors.toList());
        return catalogVoList1;
    }

    private List<CatalogVo> getCatagoryChildren(List<ProductCategoryEntity> productCategoryList, CatalogVo catalogVo1) {
        List<CatalogVo> catalogVoList2 = new ArrayList<>();
        catalogVoList2 = productCategoryList.stream().filter(f -> f.getPid().equals(catalogVo1.getId()))
                .map(m -> {
                    CatalogVo catalogVo2 = new CatalogVo();
                    catalogVo2.setId(m.getId());
                    catalogVo2.setName(m.getProductCategoryName());
                    catalogVo2.setChildren(getCatagoryChildren(productCategoryList, catalogVo2));
                    return catalogVo2;
                }).collect(Collectors.toList());
        return catalogVoList2;
    }
}